package com.sbcb.properties;

import com.google.gson.Gson;
import com.sbcb.properties.config.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/properties")
public class PropertiesController {
    @Autowired
    private DatabaseSource databaseSource;

    @Value("${browser}")
    private String browser;

    @Autowired
    private ComplicatedProperty complicatedProperty;

    @Autowired
    private YMLComplicatedProperty ymlComplicatedProperty;

    @Autowired
    private Environment environment;

    @Autowired
    private Person person;

    //http://localhost:8080/properties/simple
    @GetMapping("simple")
    public String getPropertiesFromValue(){
        return "Hello World";
    }

    //http://localhost:8080/properties/database
    @GetMapping("database")
    public String getDatabase(){
        return databaseSource.toString();
    }

    //http://localhost:8080/properties/browser
    @GetMapping("browser")
    public String getBrowser(){
        return this.browser;
    }

    //http://localhost:8080/properties/complicated
    @GetMapping("complicated")
    public String getPropertiesFromComplicatedData(){
        Gson gson = new Gson();
        StringBuilder result = new StringBuilder();
        result.append("-------------complicatedProperty--------------").append("<br/>");
        result.append(gson.toJson(complicatedProperty)).append("<br/><br/>");
        result.append("-------------ymlComplicatedProperty--------------").append("<br/>");
        result.append(gson.toJson(ymlComplicatedProperty)).append("<br/><br/>");
        result.append("-------------databaseSource--------------").append("<br/>");
        result.append(gson.toJson(databaseSource)).append("<br/><br/>");
        result.append("-------------environment--------------").append("<br/>");
        result.append(environment.getProperty("valueConfigure")).append("<br/><br/>");
        result.append("-------------Person--------------").append("<br/>");
        result.append(gson.toJson(person)).append("<br/><br/>");
        return result.toString();
    }

    //http://localhost:8080/properties/listener
    @GetMapping("listener")
    public String getPropertiesFromListener(){
        return PropertiesListenerConfig.getProperty("server.name");
    }

}
